package org.edu.eece262.taskshare;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SplashScreen extends Activity {
	
	public static final String PREFS_NAME = "TASKSHARELOGIN";

	EditText etUser;
	EditText etPassword;
	EditText etNewUser;
	EditText etNewPassword;
	EditText etNewEmail;
	EditText etNewPhone;
	EditText etNewName;

	static final int DIALOG_CREATE_USER = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);

		Button bLogin = (Button) findViewById(R.id.bLogin);
		Button bCreateUser = (Button) findViewById(R.id.bNewUser);

		etUser = (EditText) findViewById(R.id.etUser);
		etPassword = (EditText) findViewById(R.id.etPassword);

		bLogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (HTTPHelp.getActivityHTTPHelp().logIn(etUser.getText().toString(),
						etPassword.getText().toString())) {
					SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("user", etUser.getText().toString());
					editor.putString("password", etPassword.getText().toString());
					editor.commit();
					startService(new Intent(getApplicationContext(), TaskShareService.class));
					startActivity(new Intent(getApplicationContext(),
							MainMenu.class).putExtra("Username", etUser
							.getText().toString()));
				} else {
					makeToast("Login failed.");
				}

			}
		});

		bCreateUser.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_CREATE_USER);
			}
		});
	}
	
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		LayoutInflater inflater = (LayoutInflater) getApplicationContext()
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		
		View layout = inflater.inflate(R.layout.createuser,
                (ViewGroup) findViewById(R.id.cuRoot));
		
		etNewUser = (EditText) layout.findViewById(R.id.etNewUser);
		etNewPassword = (EditText) layout.findViewById(R.id.etNewPassword);
		etNewEmail = (EditText) layout.findViewById(R.id.etNewEmail);
		etNewPhone = (EditText) layout.findViewById(R.id.etNewPhone);
		etNewName = (EditText) layout.findViewById(R.id.etNewName);
		
		etNewUser.setText(etUser.getText().toString());
		etNewPassword.setText("");
		etNewEmail.setText("");
		etNewPhone.setText("");
		etNewName.setText("");
		
		builder.setMessage("Create User")
				.setView(layout)
				.setPositiveButton("Submit",
						new DialogInterface.OnClickListener() {					
							public void onClick(DialogInterface dialog, int id) {
								if (etNewUser.getText().toString().equalsIgnoreCase("")) {
									makeToast("No username entered.");
								} else if(etNewName.getText().toString().equalsIgnoreCase("")) {
									makeToast("No name entered.");
								} else if(etNewPassword.getText().toString().equalsIgnoreCase("")) {
									makeToast("No password entered.");
								} else if (etNewPhone.getText().toString().equalsIgnoreCase("")) {
									makeToast("No phone number entered.");
								} else if (etNewEmail.getText().toString().equalsIgnoreCase("")) {
									makeToast("No email address entered.");
								} else {
									if (HTTPHelp.getActivityHTTPHelp().createUser(
											etNewUser.getText().toString(),
											etNewPassword.getText().toString(),
											etNewEmail.getText().toString(),
											etNewPhone.getText().toString(),
											etNewName.getText().toString())) {
										makeToast("User created.");
										etUser.setText(etNewUser.getText().toString());
										etPassword.setText(etNewPassword.getText().toString());
										if (HTTPHelp.getActivityHTTPHelp().logIn(etUser.getText().toString(),
												etPassword.getText().toString())) {
											startActivity(new Intent(getApplicationContext(),
													MainMenu.class).putExtra("Username", etUser
													.getText().toString()));
										} else {
											makeToast("User created.\nLogin failed.");
										}
									} else {
										makeToast("Creating user failed.");
									}
								}
							}
						}).setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		return alert;
	}
	
	void makeToast(String alert) {
		Toast.makeText(getApplicationContext(), alert, Toast.LENGTH_SHORT)
				.show();
	}
}
